package com.iyao_app.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.iyao_app.R;
import com.iyao_app.entry.ProCities;
import com.iyao_app.util.MarkUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/16.
 */

public class SignLinDIAdapter extends BaseExpandableListAdapter {

    private List<ProCities> mData = new ArrayList<>();

    public SignLinDIAdapter(List<ProCities> dataList) {
        if (dataList != null) {
            mData.addAll(dataList);
        }
    }

    @Override
    public int getGroupCount() {
        return mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        if (mData == null
                || mData.size() == 0
                || mData.get(groupPosition) == null
                || mData.get(groupPosition).mans == null
                || mData.get(groupPosition).mans.size() == 0) {
            return 0;
        } else {
            return mData.get(groupPosition).mans.size();
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).mans.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group, null);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        String proCityInfo = mData.get(groupPosition).proCityInfo;
        groupViewHolder.mTvTime.setText(proCityInfo);
        if ("哎吆, 没有更多了哦~".equals(proCityInfo)) {
            groupViewHolder.mIvXiala.setVisibility(View.GONE);
        } else {
            groupViewHolder.mIvXiala.setVisibility(View.VISIBLE);
            if (isExpanded) {
                groupViewHolder.mIvXiala.setImageResource(R.mipmap.ic_shangla);
            } else {
                groupViewHolder.mIvXiala.setImageResource(R.mipmap.ic_xiala_);
            }
        }

//        groupViewHolder.mTvWeek.setText(String.format("星期%s", TimeUtils.getChineseWeek(item.createTime)));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_child_, null);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        ProCities.MansBean item = mData.get(groupPosition).mans.get(childPosition);
        childViewHolder.mSdvHead.setImageResource(MarkUtil.getId(item.typecode));
        childViewHolder.mTvName.setText(item.name);
        childViewHolder.mTvLocation.setText(item.address);

        return convertView;
    }

    public void addData(List<ProCities> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public List<ProCities> getData() {
        return mData;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    static class GroupViewHolder {
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.iv_xiala)
        ImageView mIvXiala;

        @BindView(R.id.lin_time)
        LinearLayout mLinTime;

        GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ChildViewHolder {
        @BindView(R.id.v_line)
        View mVLine;
        @BindView(R.id.sdv_head)
        SimpleDraweeView mSdvHead;
        @BindView(R.id.tv_name)
        EmojiconTextView mTvName;
        @BindView(R.id.iv_tag)
        ImageView mIvTag;
        @BindView(R.id.tv_location)
        TextView mTvLocation;
        @BindView(R.id.lin_v_1)
        LinearLayout mLinV1;
        @BindView(R.id.v_1)
        View mV1;
        @BindView(R.id.rl_root)
        LinearLayout mRlRoot;


        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
